/*
 * ConditionalExpression.java
 * 
 * last update: 30.01.2010 by Vlad Badoiu
 * 
 * author:	Vlad Badoiu(vladbadoiu@gmail.com)
 *
 * Obs:
 */


package engine;

/** 
 * the <code>?:</code> expression
 * @author Power
 * @author Alec
 * @version 1.0alpha
 * @see Expression
 */ 
class ConditionalExpression extends Expression{
	Expression condition, eTrue, eFalse;

	ConditionalExpression(NameSpace n, Expression condition, Expression eT, Expression eF) {
		super(n);
		this.condition = condition;
		eTrue = eT;
		eFalse = eF;
		length = eTrue.length >= eFalse.length ? eTrue.length : eFalse.length;
	}

	public Result evaluate()
	throws InterpretTimeException
	{

		switch( condition.evaluate().getBool()){

		case 1:
			return eTrue.evaluate();
		case 0:
			return eFalse.evaluate();
		default:      // condition evaluates to either X or Z
			BitVector result = eTrue.evaluate().getBits();
			result.combineFAbgCond(eFalse.evaluate().getBits());
			return result;
		}
	}

	void addMonitor(Monitor mon){ 
		condition.addMonitor(mon);
		eTrue.addMonitor(mon);
		eFalse.addMonitor(mon);
	}

	public boolean isLeftValue(){
		return false;
	}

	public String toString(){
		return condition + " ? " + eTrue + " : " + eFalse;
	}
}

